# -*- coding: utf-8 -*-
# @Time : 2022-06-15 15:09
# @Author : Z
# @Function :
import os, jpype

# pip install jpype==0.7.0

# 加密实例
instance = None


def init():
    global instance
    # jar包路径
    jarpath = '../../lib/encrypt.jar'
    # 获取jdk配置的环境变量
    jdkpath = os.environ['JAVA_HOME']
    print(jdkpath)

    # 启动jvm，加载jar,当有依赖的jar包存在时，要使用 "-Djava.ext.dirs参数引入"
    jpype.startJVM(jdkpath + "/jre/bin/server/jvm.dll","-Djava.class.path=%s" % jarpath,convertStrings=False)

    # 获取jar中的类
    JClass = jpype.JClass('com.testingedu.will.Encrypt')
    # 初始化类，执行构造函数
    instance = JClass()


# 加密函数，s 需要加密的参数
def encrypt(s):
    global instance
    # 调用加密函数,instance相当于java的对象
    result = str(instance.enCrypt(s))
    # 返回加密字符串
    return result


# 解密函数，s 需要解密的参数
def decrypt(s):
    global instance
    # 调用解密函数
    result = str(instance.deCrypt(s))
    # 返回解密后字符串
    return result


# 关闭jvm
def shutdowm():
    jpype.shutdownJVM()


if __name__ == '__main__':
    init()

    print("123456加密后的结果："+encrypt('123456'))
    print("解密后的结果："+decrypt(
        'GS+DuXc0KkbxwsXxEP3/RQxJ7y+bAruVIoG7M4DMe9F4DT6WRXbhGu5JLIHH0z/rTl1mimuMZ46Ymjh4eZQee8voBgzhC60WFV8gMDRfk9knXp/uj/5YcsT2t9i1HmaMJwQo65tizQeCwbWf4pzR0xt8YuiCtLiC2i9eqS9QQIIgNSHvitRpAvlqBExdZQmXLg17WQj7Nx//IrP1PH1GLP+Rbebsc72kq2L0NhT9l+2xCVWpGimQINmWUx72sr/VJBArMGwmW281zuyr9R9w6h3tDIzVEKLbLIIB9UdBuhwNk6sC9zxeHIwUOZIA6QjR+LGvykdwJQU/vihp+2lgng=='))

    import requests

    session = requests.session()
    result = session.post('http://192.168.29.137:8989/inter/HTTP/auth')
    print(result.text)
    session.headers['token'] = result.json()['token']
    result = session.post('http://192.168.29.137:8989/inter/HTTP/login',
                          data={'username': 'Will', 'password': encrypt('123456')})
    print(result.text)
    result = session.post('http://192.168.29.137:8989/inter/HTTP/logout')
    print(result.text)

